package GUI;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.Color;
import javax.swing.JList;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import logic.Book;
import logic.Message;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;

import org.apache.commons.io.FileUtils;
import javax.swing.UIManager;

//TODO just a reminder to verify JavaDoc on all methods!!

public class Add_Book extends JPanel {
	
	ImageIcon okImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resource/ok.png"));
	ImageIcon OK = new ImageIcon (okImageIcon.getImage().getScaledInstance(24, 24, java.awt.Image.SCALE_SMOOTH));
	
	ImageIcon badImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resource/bad.png"));
	ImageIcon BAD = new ImageIcon (badImageIcon.getImage().getScaledInstance(24, 24, java.awt.Image.SCALE_SMOOTH));

	private Client_GUI parent;
	private JTextField BookNameTextField;
	private JTextField EditionTextField;
	private JTextField PrintingDateTextField;
	private JTextField DatepurchasTextField;
	private JTextField NuberOfCopysTextField;
	private JTextField PlaceinLibrarytextField;
	private JTextField Author1textField;
	private JTextField Author2textField;
	private JTextField Author3textField;
	private JTextField Author4textField;
	
	private JLabel LBL_name;
	private JLabel LBL_Edition;
	private JLabel LBL_P_date;
	private JLabel LBL_date_pur;
	private JLabel LBL_Copys;
	private JLabel LBL_Place;
	private JLabel LBL_Description;
	private JLabel LBL_Author1;
	private JLabel LBL_Domain;
	private JLabel LBL_table_of_con;
	private JLabel lblNewLabel_2;
	private JLabel lblexampleF;
	
	private JTextArea discriptiontextArea;
	private JLabel author2;
	private JLabel authr3;
	private JLabel autor4;
	private JLabel lblHotBook;
	private JCheckBox hotBookChkBox;
	private JButton btnAddNewBook;
	
	private JTextField lblList[];
	
	private File selectedFile;
	private String fileSelector;
	
	private Book editBook;
	private JButton btnNewButton;
	
	private Add_Book self;
	
	ArrayList<String> categories;
	
	public Add_Book(final Client_GUI parent,Book editBook) {
		this.self = this;
		this.parent = parent;
		this.setSize(Client_GUI.MAIN_WIDTH, Client_GUI.MAIN_HEIGHT);
		this.editBook = editBook;
		setLayout(null);
		this.setBackground(UIManager.getColor("inactiveCaption"));

		JLabel booknameLBL = new JLabel("Book Name :");
		booknameLBL.setFont(new Font("Tahoma", Font.BOLD, 13));
		booknameLBL.setBounds(53, 83, 100, 14);
		add(booknameLBL);
		
		JLabel lblAddBook = new JLabel("Add Book");
		lblAddBook.setForeground(Color.RED);
		lblAddBook.setBounds(334, 40, 120, 17);
		lblAddBook.setFont(new Font("Tahoma", Font.BOLD, 17));
		add(lblAddBook);
		
		BookNameTextField = new JTextField();
	
		BookNameTextField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				parent.safeToClear = false ;
				ValidateData(false);
				
			}
		});
		
		BookNameTextField.setBounds(187, 83, 136, 20);
		add(BookNameTextField);
		BookNameTextField.setColumns(10);
		
		JLabel editionLBL = new JLabel("Edition : ");
		editionLBL.setFont(new Font("Tahoma", Font.BOLD, 13));
		editionLBL.setBounds(53, 114, 100, 14);
		add(editionLBL);
		
		EditionTextField = new JTextField();
		EditionTextField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				parent.safeToClear =false ;
				ValidateData(false);
			}
		});
		EditionTextField.setBounds(187, 114, 136, 20);
		add(EditionTextField);
		EditionTextField.setColumns(10);
		
		JLabel printingdateLBL = new JLabel("printing Date :");
		printingdateLBL.setFont(new Font("Tahoma", Font.BOLD, 13));
		printingdateLBL.setBounds(53, 139, 110, 14);
		add(printingdateLBL);
		
		PrintingDateTextField = new JTextField();
		PrintingDateTextField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				parent.safeToClear =false ;
				ValidateData(false);
			}
		});
		PrintingDateTextField.setBounds(187, 145, 136, 20);
		add(PrintingDateTextField);
		PrintingDateTextField.setColumns(10);
		
		JLabel DateofpurchaseLBL = new JLabel("Date of purchase:");
		DateofpurchaseLBL.setFont(new Font("Tahoma", Font.BOLD, 13));
		DateofpurchaseLBL.setBounds(53, 188, 136, 14);
		add(DateofpurchaseLBL);
		
		DatepurchasTextField = new JTextField();
		DatepurchasTextField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				parent.safeToClear =false ;
				ValidateData(false);
			}
		});
		DatepurchasTextField.setBounds(187, 188, 136, 20);
		add(DatepurchasTextField);
		DatepurchasTextField.setColumns(10);
		
		JLabel NumberOfCopysLBL = new JLabel("Number of copys:");
		NumberOfCopysLBL.setFont(new Font("Tahoma", Font.BOLD, 13));
		NumberOfCopysLBL.setBounds(53, 213, 157, 14);
		add(NumberOfCopysLBL);
		
		NuberOfCopysTextField = new JTextField();
		NuberOfCopysTextField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				parent.safeToClear =false ;
				ValidateData(false);
			}
		});
		NuberOfCopysTextField.setBounds(187, 216, 136, 20);
		add(NuberOfCopysTextField);
		NuberOfCopysTextField.setColumns(10);
		
		JLabel lblPlaceInLibrary = new JLabel("Place in Library:");
		lblPlaceInLibrary.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblPlaceInLibrary.setBounds(53, 262, 157, 14);
		add(lblPlaceInLibrary);
		
		PlaceinLibrarytextField = new JTextField();
		PlaceinLibrarytextField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
			
				ValidateData(false);
				parent.safeToClear =false ;
				
			}
		});
		PlaceinLibrarytextField.setBounds(187, 262, 136, 20);
		add(PlaceinLibrarytextField);
		PlaceinLibrarytextField.setColumns(10);
		
		JLabel BriefDescriptionLBL = new JLabel("Brief Description:");
		BriefDescriptionLBL.setFont(new Font("Tahoma", Font.BOLD, 13));
		BriefDescriptionLBL.setBounds(53, 304, 136, 14);
		add(BriefDescriptionLBL);
		
		btnAddNewBook = new JButton("Add new Book");
		btnAddNewBook.setFont(new Font("Tahoma", Font.BOLD, 14));
		btnAddNewBook.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				parent.safeToClear =false ;
				ValidateData(true);
			}
		});
		btnAddNewBook.setBounds(187, 449, 136, 23);
		add(btnAddNewBook);
		
		JButton btnCencel = new JButton("Cancel");
		btnCencel.setFont(new Font("Tahoma", Font.BOLD, 14));
		btnCencel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Home home = new Home(parent,parent.client.publicMessages);
				VerifyDialog verify = new VerifyDialog(parent, home,false);
			}
		});
		btnCencel.setBounds(444, 449, 136, 23);
		add(btnCencel);
		
		discriptiontextArea = new JTextArea();
		discriptiontextArea.setBackground(UIManager.getColor("inactiveCaptionBorder"));
		discriptiontextArea.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				parent.safeToClear = false ;
				ValidateData(false);
			}
		});
		
		discriptiontextArea.setWrapStyleWord(true);
		discriptiontextArea.setForeground(Color.BLACK);
		discriptiontextArea.setBounds(187, 310, 136, 109);
		add(discriptiontextArea);
		
		JLabel AuthorsLBL = new JLabel("Authors:");
		AuthorsLBL.setFont(new Font("Dialog", Font.BOLD, 13));
		AuthorsLBL.setBounds(428, 83, 128, 14);
		add(AuthorsLBL);
		
		Author1textField = new JTextField();
		Author1textField.setFont(new Font("Dialog", Font.BOLD, 13));
		Author1textField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				parent.safeToClear =false ;
				ValidateData(false);
			}
		});
		Author1textField.setBounds(555, 77, 140, 23);
		add(Author1textField);
		Author1textField.setColumns(10);
		
		Author2textField = new JTextField();
		Author2textField.setFont(new Font("Dialog", Font.BOLD, 13));
		Author2textField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				parent.safeToClear =false ;
				ValidateData(false);
			}
		});
		Author2textField.setColumns(10);
		Author2textField.setBounds(555, 104, 140, 20);
		add(Author2textField);
		
		Author3textField = new JTextField();
		Author3textField.setFont(new Font("Dialog", Font.BOLD, 13));
		Author3textField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				parent.safeToClear =false ;
				ValidateData(false);
			}
		});
		Author3textField.setColumns(10);
		Author3textField.setBounds(555, 129, 140, 20);
		add(Author3textField);
		
		JLabel lblNewLabel = new JLabel("Category:");
		lblNewLabel.setFont(new Font("Dialog", Font.BOLD, 13));
		lblNewLabel.setBounds(428, 213, 128, 29);
		add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("Table of Contents:");
		lblNewLabel_1.setFont(new Font("Dialog", Font.BOLD, 13));
		lblNewLabel_1.setBounds(428, 262, 128, 14);
		add(lblNewLabel_1);
		
		Author4textField = new JTextField();
		Author4textField.setFont(new Font("Dialog", Font.BOLD, 13));
		Author4textField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				parent.safeToClear =false ;
				ValidateData(false);
			}
		});
		Author4textField.setColumns(10);
		Author4textField.setBounds(555, 160, 140, 20);
		add(Author4textField);
		
		lblList = new JTextField[4];
		lblList[0] = Author1textField;
		lblList[1] = Author2textField;
		lblList[2] = Author3textField;
		lblList[3] = Author4textField;
		
		LBL_name = new JLabel("");
		LBL_name.setBounds(333, 83, 20, 20);
		add(LBL_name);
		
		LBL_Edition = new JLabel("");
		LBL_Edition.setBounds(333, 114, 20, 20);
		add(LBL_Edition);
		
		LBL_P_date = new JLabel("");
		LBL_P_date.setBounds(333, 145, 20, 20);
		add(LBL_P_date);
		
		LBL_date_pur = new JLabel("");
		LBL_date_pur.setBounds(333, 188, 20, 20);
		add(LBL_date_pur);
		
		LBL_Copys = new JLabel("");
		LBL_Copys.setBounds(333, 213, 20, 20);
		add(LBL_Copys);
		
		LBL_Place = new JLabel("");
		LBL_Place.setBounds(333, 262, 20, 20);
		add(LBL_Place);
		
		LBL_Description = new JLabel("");
		LBL_Description.setBounds(333, 313, 20, 20);
		add(LBL_Description);
		
		LBL_Author1 = new JLabel("");
		LBL_Author1.setFont(new Font("Dialog", Font.BOLD, 13));
		LBL_Author1.setBounds(705, 77, 20, 20);
		add(LBL_Author1);
		
		LBL_Domain = new JLabel("");
		LBL_Domain.setFont(new Font("Dialog", Font.BOLD, 13));
		LBL_Domain.setBounds(705, 216, 20, 20);
		add(LBL_Domain);
		
		LBL_table_of_con = new JLabel("");
		LBL_table_of_con.setBounds(705, 262, 20, 20);
		add(LBL_table_of_con);
		
		lblNewLabel_2 = new JLabel("[example: 2000-02-20]");
		lblNewLabel_2.setFont(new Font("Tahoma", Font.PLAIN, 10));
		lblNewLabel_2.setBounds(203, 169, 120, 14);
		add(lblNewLabel_2);
		
		lblexampleF = new JLabel("[example: F-25]");
		lblexampleF.setFont(new Font("Tahoma", Font.PLAIN, 10));
		lblexampleF.setBounds(219, 247, 120, 14);
		add(lblexampleF);
		
		author2 = new JLabel("");
		author2.setFont(new Font("Dialog", Font.BOLD, 13));
		author2.setBounds(705, 108, 20, 20);
		add(author2);
		
		authr3 = new JLabel("");
		authr3.setFont(new Font("Dialog", Font.BOLD, 13));
		authr3.setBounds(705, 129, 20, 20);
		add(authr3);
		
		autor4 = new JLabel("");
		autor4.setFont(new Font("Dialog", Font.BOLD, 13));
		autor4.setBounds(705, 160, 20, 20);
		add(autor4);
		
		lblHotBook = new JLabel("Hot Book:");
		lblHotBook.setFont(new Font("Dialog", Font.BOLD, 13));
		lblHotBook.setBounds(428, 194, 128, 14);
		add(lblHotBook);
		
		hotBookChkBox = new JCheckBox("Hot Book");
		hotBookChkBox.setBackground(UIManager.getColor("inactiveCaption"));
		hotBookChkBox.setFont(new Font("Dialog", Font.BOLD, 13));
		hotBookChkBox.setBounds(555, 188, 140, 23);
		add(hotBookChkBox);
		
		btnNewButton = new JButton("Select");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CategorySelector cat = new CategorySelector(self,categories,0);
			}
		});
		btnNewButton.setBounds(555, 217, 140, 23);
		add(btnNewButton);
		
		JButton button = new JButton("Select");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
		           JFileChooser chooser = new JFileChooser();  
		           chooser.showOpenDialog(null);                
		           selectedFile = chooser.getSelectedFile();               
		           String filename = selectedFile.getName();     
			}
			
		});
		button.setBounds(555, 259, 140, 23);
		add(button);
		
		if (editBook != null) {
			LoadBook(editBook);
			btnAddNewBook.setText("Update Book");
		}
	}
	
	
	public void SetCategories(ArrayList<String> cat){
		this.categories = cat;
		ValidateData(false);
	}
	
	private void LoadBook(Book editBook){
		BookNameTextField.setText(editBook.getname());
		EditionTextField.setText(String.valueOf(editBook.getedition()));
		PrintingDateTextField.setText(editBook.getprintDate());
		DatepurchasTextField.setText(editBook.getpurchaseDate());
		NuberOfCopysTextField.setText(String.valueOf(editBook.getcopies()));
		PlaceinLibrarytextField.setText(editBook.getlocation());
		discriptiontextArea.setText(editBook.getdescription());
		if (editBook.gethotBook() == 1) hotBookChkBox.setSelected(true);
		this.categories = editBook.getcategories();
		
		
		ArrayList<String> authList = editBook.getAuthors();
		for(int i=0;i<authList.size(); i++)
			lblList[i].setText(authList.get(i));
	}
	
	
	
	private void ValidateData(boolean fromOK){
		
		
		boolean valid = true;
		
		String temp; // the string to be tested on each test
		
		//Validate Book name
		if(!BookNameTextField.getText().equals("")) LBL_name.setIcon(OK); 
		else LBL_name.setIcon(null);
		
		//Validate Book name
		if(!EditionTextField.getText().equals("")){
			try{
				char c = EditionTextField.getText().charAt(0); //Parse int also enables -/+ sign at begining. We dont want it
				if ( c == '-') {
					LBL_Edition.setIcon(BAD);
					valid = false;
				}else LBL_Edition.setIcon(OK);
				
				Integer.parseInt(EditionTextField.getText());	
			}catch(NumberFormatException e){ //contains different than int
				LBL_Edition.setIcon(BAD);
				valid = false;
			}
		}else LBL_Edition.setIcon(null);
		
		
		//validate printig date
		temp = PrintingDateTextField.getText();
		if(temp.length()== 10)
		try
		{
			if ((temp.charAt(4) == '-' && temp.charAt(7) == '-') || (temp.charAt(4) == '/' && temp.charAt(7) == '/') || (temp.charAt(4) == '.' && temp.charAt(7) == '.')) 
				LBL_P_date.setIcon(OK);
			else {
				LBL_P_date.setIcon(BAD);
				valid = false;
			}
			
			int year = Integer.parseInt(temp.substring(0, 4));
			int month = Integer.parseInt(temp.substring(5, 7));
			int day = Integer.parseInt(temp.substring(8, temp.length()));

			Calendar validator = Calendar.getInstance();
			validator.set(year, month-1, day);
			validator.setLenient(false);
			validator.getTime();
			
		}catch(NumberFormatException e){ //Date contains chars different than int
			LBL_P_date.setIcon(BAD);
			valid = false;
		}catch(Exception e){ //date is invalid
			LBL_P_date.setIcon(BAD);
			valid = false;
		}
		else if(!temp.equals("")) { //date contains anything else
			LBL_P_date.setIcon(BAD);
			valid = false;
		}
		
		
		//validate buying  date
		temp = DatepurchasTextField.getText();
		if(temp.length() == 10)
			try
			{
				if ((temp.charAt(4) == '-' && temp.charAt(7) == '-') || (temp.charAt(4) == '/' && temp.charAt(7) == '/') || (temp.charAt(4) == '.' && temp.charAt(7) == '.')) 
					LBL_date_pur.setIcon(OK);
				else {
					LBL_date_pur.setIcon(BAD);
					valid = false;
				}
								
				int year = Integer.parseInt(temp.substring(0, 4));
				int month = Integer.parseInt(temp.substring(5, 7));
				int day = Integer.parseInt(temp.substring(8, temp.length()));

				Calendar validator = Calendar.getInstance();
				validator.set(year, month-1, day);
				validator.setLenient(false);
				validator.getTime();
				
			}catch(NumberFormatException e){//Date contains chars different than int
				LBL_date_pur.setIcon(BAD);
				valid = false;
			}catch(Exception e){
				LBL_date_pur.setIcon(BAD);//date is invalid
				valid = false;
			}
			else if(!temp.equals("")) {//date contains anything else
				LBL_date_pur.setIcon(BAD);
				valid = false;
			}
		
		//validate the number of copys
		temp = NuberOfCopysTextField.getText();
		if (!temp.equals(""))
		try{
			char c = temp.charAt(0); //Parse int also enables -/+ sign at begining. We dont want it
			if ( c == '-') {
				LBL_Copys.setIcon(BAD);
				valid = false;
			}
			else LBL_Copys.setIcon(OK);

			
			Integer.parseInt(temp);	
		}catch(NumberFormatException e){ //contains different than int
			LBL_Copys.setIcon(BAD);
			valid = false;
		}
		
		//Validate the descrition
		temp = discriptiontextArea.getText();
		if (!temp.equals("")) LBL_Description.setIcon(OK);
		else LBL_Description.setIcon(null);
	
		
		//Validate author1 name
		if(!Author1textField.getText().equals("")) LBL_Author1.setIcon(OK); 
		else LBL_Author1.setIcon(null);
		
		//Validate author2 name
		if(!Author2textField.getText().equals("")) author2.setIcon(OK); 
		else author2.setIcon(null);
		
		//Validate author3 name
		if(!Author3textField.getText().equals("")) authr3.setIcon(OK); 
		else authr3.setIcon(null);
		
		//Validate author4 name
		if(!Author4textField.getText().equals("")) autor4.setIcon(OK); 
		else autor4.setIcon(null);
		
		
		if(PlaceinLibrarytextField.getText().equals("")){
			LBL_Place.setIcon(BAD);
			valid = false;
		}else LBL_Place.setIcon(OK);
					
		//TODO verify copies & and rented copies if reducing amount of copies
		
		if (fromOK){
			if (BookNameTextField.getText().equals("")){
				LBL_name.setIcon(BAD);
				valid = false;
			}
			if (EditionTextField.getText().equals("")){
				LBL_Edition.setIcon(BAD);
				valid = false;
			}
			if (DatepurchasTextField.getText().equals("")){
				LBL_date_pur.setIcon(BAD);
				valid = false;
			}
			if (PrintingDateTextField.getText().equals("")){
				LBL_P_date.setIcon(BAD);
				valid = false;
			}
			if (NuberOfCopysTextField.getText().equals("")){
				LBL_Copys.setIcon(BAD);
				valid = false;
			}
			if (PlaceinLibrarytextField.getText().equals("")){
				LBL_Place.setIcon(BAD);
				valid = false;
			}
			if (discriptiontextArea.getText().equals("") ){
				LBL_Description.setIcon(BAD);
				valid = false;
			}
			if (Author1textField.getText().equals("")&& Author2textField.getText().equals("")&& Author3textField.getText().equals("")&& Author4textField.getText().equals("")){
				LBL_Author1.setIcon(BAD);
				valid = false;
			}

			if (categories == null){
				LBL_Domain.setIcon(BAD);
				valid = false;
			}else LBL_Domain.setIcon(OK);
			
			if(selectedFile == null){
				LBL_table_of_con.setIcon(BAD);
				valid = false;
			}else LBL_table_of_con.setIcon(OK);
			
			
		}else{
			LBL_Domain.setIcon(null);
		}
		
		
		if (valid && fromOK){
			ArrayList<String> index = new ArrayList<String>();
			ArrayList<Object> data = new ArrayList<Object>();
	
			//BID is auto-increment
			
			index.add("NAME");
			data.add(BookNameTextField.getText());
					
			
			ArrayList<String> authorList = new ArrayList<String>();
			if(!Author1textField.getText().equals("")) authorList.add(Author1textField.getText());
			if(!Author2textField.getText().equals("")) authorList.add(Author2textField.getText());
			if(!Author3textField.getText().equals("")) authorList.add(Author3textField.getText());
			if(!Author4textField.getText().equals("")) authorList.add(Author4textField.getText());
			String author = authorList.get(0);
			for(int i=1; i< authorList.size(); i++){
				author += " , " + authorList.get(i);
			}
			
			index.add("AUTHORS");
			data.add(author);
			
			index.add("EDITION");
			data.add(EditionTextField.getText());
			
			index.add("PRINT_DATE");
			data.add(PrintingDateTextField.getText());
			
			index.add("PURCHASE_DATE");
			data.add(DatepurchasTextField.getText());
			
			index.add("HOT_BOOK");
			if (hotBookChkBox.isSelected()) data.add("1");
			else data.add("0");
			
			index.add("COPIES");
			data.add(NuberOfCopysTextField.getText());

			index.add("DESCRIPTION");
			data.add(discriptiontextArea.getText());
			
			index.add("LOCATION");
			data.add(PlaceinLibrarytextField.getText());

			byte[] fileByte = null;
		    try {
				fileByte = FileUtils.readFileToByteArray(selectedFile);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			index.add("BOOK_INDEX");
			data.add(fileByte);
			
			index.add("CATEGORY");
			data.add(categories);
			
			int action;
			if (editBook == null) action = Message.ADD_BOOK_REQUEST;
				else{
					index.add("BID");
					data.add(editBook.getBID());
					action = Message.UPDATE_BOOK_REQUEST;
				}


			Message toSend = new Message(index, data, action);
			
			if (parent.sendMessageToServer(toSend) == 1){//message sent successfully
				parent.SetLoading(true);
								
			}else{//message send failed
				//TODO verify extream situation
			}
			
		
	}
	
	
}
}

